from typing import TypeVar, Generic, List

from sqlmodel import SQLModel, Field
from datetime import datetime

from fastapi import Query

# 定义类型变量 T
T = TypeVar('T')


class TimestampMixin(SQLModel):
    create_time: datetime = Field(default_factory=datetime.utcnow, nullable=False)
    update_time: datetime = Field(default_factory=datetime.utcnow, sa_column_kwargs={'onupdate': datetime.utcnow},
                                  nullable=False)


# 自定义 Query 类型，将空字符串转换为 None
def EmptyStrToNone(query: str = Query(default=None)):
    return None if query == '' else query


class PageParams(SQLModel):
    page_no: int = Field(1, ge=1, description='Page Number')
    page_size: int = Field(20, gt=0, le=100, description='Page Size')


class PageListOut(SQLModel, Generic[T]):
    lists: list[T]
    count: int
    page_no: int
    page_size: int
